package com.elitel.frame.base.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.elitel.common.messageQueue.RabbitMQReceiver;
import com.elitel.common.utils.XCommonUtils;
import com.elitel.frame.main.entity.state.TimePushActivemq;
import com.elitel.frame.main.entity.vo.ServerStatistics;

/**
 * 
 * <pre>@ProjectName：frame-base-web    
 * @ClassName：PerformanceMonitorController    
 * @Description: TODO(性能监控模块服务)    
 * @author：FanWenquan  
 * @date：2017年9月21日 下午3:55:46       
 * @version </pre>
 */
@Controller
@RequestMapping("/system/monitor")
public class PerformanceMonitorController {
	
	private static final Logger logger = LoggerFactory.getLogger(PerformanceMonitorController.class);

	@Autowired
	private RabbitMQReceiver rabbitMQReceiver;

	@Autowired
	private TimePushActivemq timePushActivemq;
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	/**
	 * <pre>showInformation(推送前台性能信息)   
	 * @author：FanWenquan    
	 * @date：2017年9月21日 下午3:54:43   
	 * @return</pre>
	 */
	@RequestMapping("/showInformation")
	@ResponseBody
	public Object showInformation(){
		StringBuilder strtxt = new StringBuilder();
		List<ServerStatistics> serverRecorderList = rabbitMQReceiver.offerAll();

		for(ServerStatistics serverRecorder : serverRecorderList){
			strtxt.append("<p>====性能日志Begin(当前日志记录时间:"+ sdf.format(new Date(serverRecorder.serverStartTime))+")====<br/>");
			strtxt.append("服务名称(ServiceID):&nbsp" + tenSpaceForHtml() + serverRecorder.getServerName()+"<br/>");
			strtxt.append("服务执行时间:    &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp"            + tenSpaceForHtml() + serverRecorder.getServerTime()+"ms<br/>");
			strtxt.append("主函数执行时间:  &nbsp &nbsp &nbsp  &nbsp"                       + tenSpaceForHtml() + serverRecorder.getSqlTime()+"ms<br/>");
			if(XCommonUtils.isNotEmpty(serverRecorder.getSql()))
				strtxt.append("主函数相应SQL:   &nbsp &nbsp &nbsp &nbsp"                        + tenSpaceForHtml() + serverRecorder.getSql()+"<br/>");

			strtxt.append("===日志End===<p><br/>");
		}

		return strtxt;
	}

	private String tenSpaceForHtml(){
		return " &nbsp &nbsp &nbsp &nbsp &nbsp ";

	}
	
	/**
	 * <pre>setActiveMqPush(消息推送开关)   
	 * @author：FanWenquan    
	 * @date：2017年9月26日 下午2:57:58   
	 * @param id</pre>
	 */
	@RequestMapping("/setActiveMqPush")
	@ResponseBody
	public void setActiveMqPush(String id){
		timePushActivemq.setPushSwitch(Integer.valueOf(id));
	}
	
	/**
	 * <pre>querytimeInterval(获取性能监控时间)   
	 * @author：FanWenquan    
	 * @date：2017年9月26日 下午3:16:05   
	 * @return</pre>
	 */
	@RequestMapping("/querytimeInterval")
	@ResponseBody
	public Object querytimeInterval(){
		Long time = null;
		try {
		     time = timePushActivemq.getTimeInterval();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return time;
	}
	
	/**
	 * <pre>settimeInterval(设置性能监控时间)   
	 * @author：FanWenquan    
	 * @date：2017年9月26日 下午3:16:28   
	 * @param timeid</pre>
	 */
	@RequestMapping("/settimeInterval")
	@ResponseBody
	public void settimeInterval(String timeid){
		try {
		    timePushActivemq.setTimeInterval(Long.valueOf(timeid));
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
	}
}
